/*
 * InsertTrade.java
 *
 * Created on September 7, 2007, 5:44 PM
 */

package com.app;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.sql.Date;

import com.db.processror.TradeProcessor;
import com.trade.TradeBean;

/**
 *
 * @author  gauravf
 */
public class InsertTrade extends javax.swing.JFrame {
    
    /**
	 * 
	 */
	private static final long serialVersionUID = 5426700830088996458L;
	/** Creates new form InsertTrade */
    public InsertTrade() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">
    private void initComponents() {

        tradeIdLabel = new javax.swing.JLabel();
        dateLabel = new javax.swing.JLabel();
        stockLabel = new javax.swing.JLabel();
        actionLabel = new javax.swing.JLabel();
        qtyLabel = new javax.swing.JLabel();
        priceLabel = new javax.swing.JLabel();
        tradeValueLabel = new javax.swing.JLabel();
        brokerageLabel = new javax.swing.JLabel();
        orderRefLabel = new javax.swing.JLabel();
        settlementLabel = new javax.swing.JLabel();
        segmentLabel = new javax.swing.JLabel();
        dpIdClientDpIdLabel = new javax.swing.JLabel();
        exchangeLabel = new javax.swing.JLabel();
        tradeId = new javax.swing.JTextField();
        date = new javax.swing.JTextField();
        stock = new javax.swing.JTextField();
        action = new javax.swing.JTextField();
        qty = new javax.swing.JTextField();
        price = new javax.swing.JTextField();
        tradeValue = new javax.swing.JTextField();
        brokerage = new javax.swing.JTextField();
        orderRef = new javax.swing.JTextField();
        settlement = new javax.swing.JTextField();
        segment = new javax.swing.JTextField();
        dpIdClientDpId = new javax.swing.JTextField();
        exchange = new javax.swing.JTextField();
        insert = new javax.swing.JButton();
        reset = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        tradeIdLabel.setText("TradeID");

        dateLabel.setText("Date");

        stockLabel.setText("Stock");

        actionLabel.setText("Action");

        qtyLabel.setText("Qty");

        priceLabel.setText("Price");

        tradeValueLabel.setText("TradeValue");

        brokerageLabel.setText("Brokerageincltaxes");

        orderRefLabel.setText("OrderRef");

        settlementLabel.setText("Settlement");

        segmentLabel.setText("Segment");

        dpIdClientDpIdLabel.setText("DPIdClientDPId");

        exchangeLabel.setText("Exchange");

        insert.setText("Insert");


        insert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertActionPerformed(evt);
            }
        });

        reset.setText("Reset");


        reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");


        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this.getContentPane());
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(28, 28, 28)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tradeIdLabel)
                            .addComponent(dateLabel)
                            .addComponent(stockLabel)
                            .addComponent(actionLabel)
                            .addComponent(qtyLabel)
                            .addComponent(priceLabel)
                            .addComponent(tradeValueLabel)
                            .addComponent(brokerageLabel)
                            .addComponent(orderRefLabel)
                            .addComponent(settlementLabel)
                            .addComponent(segmentLabel)
                            .addComponent(dpIdClientDpIdLabel)
                            .addComponent(exchangeLabel)
                            .addComponent(reset))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tradeId, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(date, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(stock, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(action, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(qty, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(price, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(tradeValue, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(brokerage, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(orderRef, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(settlement, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(segment, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(dpIdClientDpId, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(exchange, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(insert, javax.swing.GroupLayout.Alignment.CENTER)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(71, 71, 71)
                        .addComponent(cancel)))
                .addContainerGap(59, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {action, actionLabel, brokerage, brokerageLabel, date, dateLabel, dpIdClientDpId, dpIdClientDpIdLabel, exchange, exchangeLabel, insert, orderRef, orderRefLabel, price, priceLabel, qty, qtyLabel, reset, segment, segmentLabel, settlement, settlementLabel, stock, stockLabel, tradeId, tradeIdLabel, tradeValue, tradeValueLabel});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tradeId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tradeIdLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dateLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(stock, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(stockLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(action, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(actionLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(qty, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(qtyLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(price, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(priceLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tradeValue, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(tradeValueLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(brokerage, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(brokerageLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(orderRef, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(orderRefLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(settlement, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(settlementLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(segment, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(segmentLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dpIdClientDpId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(dpIdClientDpIdLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(exchange, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(exchangeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(reset)
                    .addComponent(insert))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(cancel)
                .addContainerGap(14, Short.MAX_VALUE))
        );

    }// </editor-fold>

private void cancelActionPerformed(java.awt.event.ActionEvent evt) {
    this.dispose();
}

private void insertActionPerformed(java.awt.event.ActionEvent evt) {
    TradeBean tb = new TradeBean();
    tb.setAction(this.action.getText());
    tb.setBrokerageincltaxes(Double.parseDouble(this.brokerage.getText()==null||this.brokerage.getText().length()==0?"0":this.brokerage.getText()));
    java.sql.Date tempDate = new Date(new java.util.Date().getTime());
    if(this.date.getText()!=null && this.date.getText().length()!=0){
    	tempDate=java.sql.Date.valueOf(this.date.getText());
    }
    tb.setDate(tempDate);
    tb.setDPIdClientDPId(this.dpIdClientDpId.getText());
    tb.setExchange(this.exchange.getText());
    tb.setOrderRef(this.orderRef.getText());
    tb.setPrice(Double.parseDouble(this.price.getText()==null||this.price.getText().length()==0?"0":this.price.getText()));
    tb.setQty(Integer.parseInt(this.qty.getText()==null||this.qty.getText().length()==0?"0":this.qty.getText()));
    tb.setSegment(this.segment.getText());
    tb.setSettlement(this.settlement.getText());
    tb.setStock(this.stock.getText());
    tb.setTradeID(Integer.parseInt(this.tradeId.getText()==null||this.tradeId.getText().length()==0?"0":this.tradeId.getText()));
    tb.setTradeValue(Double.parseDouble(this.tradeValue.getText()==null||this.tradeValue.getText().length()==0?"0":this.tradeValue.getText()));
    try {
		TradeProcessor.insertTrade(tb);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

private void resetActionPerformed(java.awt.event.ActionEvent evt) {
    Class<? extends InsertTrade> c = this.getClass();
    Field[] f = c.getDeclaredFields();
    for(Field m:f){
    	try {
			if (m.get(this) instanceof javax.swing.JTextField) {
				javax.swing.JTextField textBox = (javax.swing.JTextField) m.get(this);
				textBox.setText("");
			}
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
    }
}
    
    
    // Variables declaration - do not modify
    private javax.swing.JTextField action;
    private javax.swing.JLabel actionLabel;
    private javax.swing.JTextField brokerage;
    private javax.swing.JLabel brokerageLabel;
    private javax.swing.JButton cancel;
    private javax.swing.JTextField date;
    private javax.swing.JLabel dateLabel;
    private javax.swing.JTextField dpIdClientDpId;
    private javax.swing.JLabel dpIdClientDpIdLabel;
    private javax.swing.JTextField exchange;
    private javax.swing.JLabel exchangeLabel;
    private javax.swing.JButton insert;
    private javax.swing.JTextField orderRef;
    private javax.swing.JLabel orderRefLabel;
    private javax.swing.JTextField price;
    private javax.swing.JLabel priceLabel;
    private javax.swing.JTextField qty;
    private javax.swing.JLabel qtyLabel;
    private javax.swing.JButton reset;
    private javax.swing.JTextField segment;
    private javax.swing.JLabel segmentLabel;
    private javax.swing.JTextField settlement;
    private javax.swing.JLabel settlementLabel;
    private javax.swing.JTextField stock;
    private javax.swing.JLabel stockLabel;
    private javax.swing.JTextField tradeId;
    private javax.swing.JLabel tradeIdLabel;
    private javax.swing.JTextField tradeValue;
    private javax.swing.JLabel tradeValueLabel;
    // End of variables declaration
	/**
	 * @return the action
	 */
	public javax.swing.JTextField getAction() {
		return action;
	}

	/**
	 * @param action the action to set
	 */
	public void setAction(javax.swing.JTextField action) {
		this.action = action;
	}

	/**
	 * @return the stock
	 */
	public javax.swing.JTextField getStock() {
		return stock;
	}

	/**
	 * @param stock the stock to set
	 */
	public void setStock(javax.swing.JTextField stock) {
		this.stock = stock;
	}
    
}
